import { HttpParams } from '@angular/common/http';

export class NameLabel {
  category!: string;
  name!: string;
  label!: string;

  constructor(data?: Partial<NameLabel>) {
    Object.assign(this, data);
  }
}

/** 基础实体 */
export class BaseEntity {
  id!: string;
  version!: number;

  constructor(data?: Partial<BaseEntity>) {
    Object.assign(this, data);
  }
}

export class AuditEntity extends BaseEntity {
  createdBy!: string;
  lastModifiedBy!: string;
  createdDate!: Date;
  lastModifiedDate!: Date;

  constructor(data?: Partial<AuditEntity>) {
    super(data);
    Object.assign(this, data);

    if (this.createdDate) {
      this.createdDate = new Date(this.createdDate);
    }
    if (this.lastModifiedDate) {
      this.lastModifiedDate = new Date(this.lastModifiedDate);
    }
  }
}

export class Pagable {
  page: number = 1;
  size: number = 10;
  sort: Sort[] = [];

  constructor(data?: Partial<Pagable>) {
    Object.assign(this, data);
  }

  /** 重新分页 */
  public reset(): void {
    this.page = 1;
  }

  public get httpParams(): HttpParams {
    let rslt: HttpParams = new HttpParams();

    rslt = rslt.set('page', this.page).set('size', this.size);
    if (this.sort != null) {
      this.sort.forEach(s => {
        rslt = rslt.set('sort', `${s.property},${s.direction}`);
      });
    }

    return rslt;
  }
}

export interface Sort {
  direction: string;
  property: string;
}

export class Page<T> {
  last: boolean = true;
  totalPages: number = 0;
  totalElements: number = 0;
  number: number = 1;
  size: number = 10;
  first: boolean = false;
  numberOfElements: number = 0;
  content: T[] = [];

  constructor(data?: Partial<Page<T>>) {
    Object.assign(this, data);
  }
}
